package com.h3c.ptability.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.h3c.ptability.bo.ElectronicSignBO;
import com.h3c.ptability.dto.electronicSign.PtEsignSceneDTO;
import com.h3c.ptability.dto.electronicSign.PtEsignSceneTemplateDTO;
import com.h3c.ptability.entity.PtEsignScene;
import com.h3c.ptability.service.IPtEsignSceneService;
import com.h3c.ptability.support.ResultData;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * 电子签实际场景定义控制层
 *
 * @author chenyifan
 * @since 2022-05-19 10:59:57
 */
@Slf4j
@RestController
@AllArgsConstructor
@CrossOrigin
@RequestMapping("/ptEsignScene")
public class PtEsignSceneController {
    /**
     * 服务对象
     */
    private final IPtEsignSceneService ptEsignSceneService;

    @GetMapping("/getPageEsignScene")
    public ResultData<IPage<PtEsignSceneDTO>> pageEsignScene(PtEsignSceneDTO sceneDTO,
                                                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return ResultData.data(ptEsignSceneService.pageEsignScene(new Page<>(pageNum, pageSize), sceneDTO));
    }

    @GetMapping("/getEsignSceneList")
    public ResultData<List<PtEsignScene>> getEsignSceneList(@RequestParam String appId,
                                                            @RequestParam(required = false) Integer sceneType) {
        return ResultData.data(ptEsignSceneService.getEsignSceneList(appId, sceneType));
    }

    @GetMapping("/selectSignById")
    public ResultData<PtEsignSceneDTO> selectSignById(@ModelAttribute PtEsignSceneDTO sceneDTO) {
        PtEsignSceneDTO esignSceneDTO = ptEsignSceneService.selectSignById(sceneDTO);
        return ResultData.data(esignSceneDTO);
    }

    @PostMapping("/updateByEsignScene")
    public ResultData<Boolean> updateByEsignScene(@RequestBody PtEsignSceneDTO sceneDTO) {
        Boolean aBoolean = ptEsignSceneService.updateByEsignScene(sceneDTO);
        return ResultData.data(aBoolean);
    }

    @PostMapping("/deleteEsignScene")
    public ResultData<Boolean> deleteEsignScene(@RequestBody PtEsignSceneDTO sceneDTO) {
        Boolean aBoolean = ptEsignSceneService.deleteEsignScene(sceneDTO);
        return ResultData.data(aBoolean);
    }

    /**
     * 匹配场景预览需要签署的文件
     *
     * @param electronicSignBO
     * @return
     */
    @PostMapping("/matchSceneFiles")
    public ResultData<HashMap<String, List<PtEsignSceneTemplateDTO>>> matchSceneFiles(@Valid @RequestBody ElectronicSignBO electronicSignBO) {
        try {
            return ResultData.data(ptEsignSceneService.matchSceneFiles(electronicSignBO));
        } catch (Exception e) {
            log.error("匹配场景预览签署失败:{}", e);
            return ResultData.fail("匹配场景预览签署失败");
        }
    }

    /**
     * 签署按钮回显
     *
     * @param sceneId
     * @return
     */
    @GetMapping(value = "/directSignButton")
    public ResultData<Boolean> directSignButton(@RequestParam String sceneId) {
        PtEsignScene esignScene = ptEsignSceneService.getById(sceneId);
        if (Objects.isNull(esignScene)) {
            return ResultData.data(false);
        }
        if (esignScene.getDirectlyLaunch() == 1) {
            return ResultData.data(true);
        }
        return ResultData.data(false);
    }
}

